var eyes_global_config_string = "";
// 是否认真读
var eyes_readLong = true;

// 执行的次数
var eyes_times = 100;

// 间隔时长 默认7s
var eyes_waitTime = 7 * 1000;
var eyes_waitTime2 = 10 * 1000;

var eyes_readDetailNumbers = 3;

var hasClicked = 0;

var hasClickedNode = [];

var eyes_scrollForwardTimes = 0;

var EYES_TOTAL_SINGLE_WAIT_TIMES = 50;
var EYES_TOTAL_SINGLE_WAIT_TIME = 15;

/**
 *
 */
eye_readConfig = function () {
    if (readConfigString("eyes_all_config_") != null && readConfigString("eyes_all_config_") !== "") {
        eyes_global_config_string = readConfigString("eyes_all_config_")
    }
    if (eyes_global_config_string === ""
        || eyes_global_config_string === null
        || eyes_global_config_string === undefined
        || eyes_global_config_string === "undefined") {
        loge("配置错误!");
        exit();
    }
    var json = JSON.parse(eyes_global_config_string);

    // 是否认真读
    eyes_readLong = json["eye_readLong"] !== undefined;
    logi("是否认真读 {}", eyes_readLong);

    // 文章的标题
    eyes_readDetailNumbers = convertNumFromPage(json["eye_readDetailNumbers"], 0);
    logi("循环文章数量 {} 篇", eyes_readDetailNumbers);
    if (eyes_readDetailNumbers <= 1) {
        loge("循环文章数量最小为 2");
        exit();
    }

    // 间隔时长 默认7s
    eyes_waitTime = convertNumFromPage1000(json["eye_single_waitTime1"], 7);
    eyes_waitTime2 = convertNumFromPage1000(json["eye_single_waitTime2"], 10);
    logi("单篇文章浏览随机时间 {}", eyes_waitTime / 1000 + "-" + eyes_waitTime2 / 1000 + "秒");

    // 执行的次数
    var eye_article_times = convertNumFromPage(json["eye_article_times"], 500);
    eyes_times = parseInt(eye_article_times);
    logi("单篇文章小眼睛 {} ", eyes_times + " 个");

    // 刷满次数超过 {} 等待 {}秒
    var eye_total_single_wait_times_ = convertNumFromPage(json["eye_total_single_wait_times"], 50);
    EYES_TOTAL_SINGLE_WAIT_TIMES = parseInt(eye_total_single_wait_times_);
    var eye_total_single_wait_time_ = convertNumFromPage1000(json["eye_total_single_wait_time"], 15);
    EYES_TOTAL_SINGLE_WAIT_TIME = parseInt(eye_total_single_wait_time_);
    logi("刷满次数超过 {}个 等待 {}秒", EYES_TOTAL_SINGLE_WAIT_TIMES, EYES_TOTAL_SINGLE_WAIT_TIME / 1000);
};

eyes_getNid = function (node) {
    try {
        if (node != null) {
            return node.nid;
        }
    } catch (e) {
        loge("split error {}", e);
    }
};

eyes_readHard = function () {
    var end;
    do {
        // 判断是否是图文还是视频
        var isArticleDetailNormalPage = judge_isArticleDetailNormalPage();
        if (isArticleDetailNormalPage) {
            moveToScreenHeightHalf(screenHeight * 0.8);
            end = pkgMatch("^((?!com.android.systemui).)*$")
                .clz("android.widget.TextView")
                .textMatch("THE END")
                .getOneNodeInfo("1000");
            end = end === null ? pkgMatch("^((?!com.android.systemui).)*$")
                .clz("android.widget.TextView")
                .textMatch("这是一片荒草地")
                .getOneNodeInfo("1000") : end;

        } else {
            sleep(4000);
        }
        sleep(500);
    } while (end === null);
    sleep(1000);
};

eyes_loop = function (index) {
    var targetPage = pkgMatch("^((?!com.android.systemui).)*$")
        .clz("android.widget.FrameLayout")
        .depth(17)
        .visible(true)
        .getNodeInfo(5000);

    if (targetPage === null) {
        logw("没有找到合适的文章");
        exit();
    } else {
        var targetLength = targetPage.length;
        for (var i = 0; i < targetLength; i++) {
            if (hasClickedNode.indexOf(targetPage[i].nid) !== -1) {
                continue;
            }

            sleep(1000);
            targetPage[i].clickCenter();

            // 判断是否在图文或者视频
            // var isArticleDetailPage = judge_isArticleDetailPage();
            // if (!isArticleDetailPage) {
            //     logw("不在文章详情~");
            //     continue;
            // }
            hasClicked++;
            EYES_TOTAL_TIMES++;
            hasClickedNode.push(targetPage[i].nid);

            // 认真读
            logi("第{}组中的第{}篇文章", index, hasClicked);
            if (eyes_readLong) {
                logi("认真读..");
                eyes_readHard();
            } else {
                logi("不认真读..");
                eyes_notReadAlong();
            }

            back();
            sleep(1500);
            //判断是否为 个人主页
            // product_detail_isBackSuccess_loop();

            if (eyes_readDetailNumbers <= hasClicked) {
                logi("ヾ(•ω•`)o");
                break;
            }
        }

    }
};

/**
 * 不认真读的处理方式
 */
eyes_notReadAlong = function () {
    var isArticleDetailNormalPage = judge_isArticleDetailNormalPage();
    if (isArticleDetailNormalPage) {
        toast("图文详情");
        sleep(1000);
        var time = getRandomTimeToSleep(eyes_waitTime, eyes_waitTime2);
        var res = (time - 1000) / 1000 / 2;
        for (var i = 0; i < res; i++) {
            sleep(1000);
            moveToScreenHeightHalf(screenHeight * 0.7);
        }
        sleep(2000);
    } else {
        toast("视频文章，等待5秒");
        sleep(5000);
    }
};

eyes_scrollForward = function () {
    try {
        return pkgMatch("^((?!com.android.systemui).)*$")
            .scrollable(true)
            .clz("androidx.recyclerview.widget.RecyclerView")
            .depth(16)
            .getOneNodeInfo(2000)
            .scrollForward();
    } catch (e) {
        loge("product_list_scrollForward error {}", e);
        return false;
    }
};

eyes_scrollBackward = function () {
    try {
        return pkgMatch("^((?!com.android.systemui).)*$")
            .scrollable(true)
            .clz("androidx.recyclerview.widget.RecyclerView")
            .depth(16)
            .getOneNodeInfo(2000)
            .scrollBackward();
    } catch (e) {
        loge("product_list_scrollForward error {}", e);
        return false;
    }
};

/**
 * 主程序
 */
var EYES_TOTAL_TIMES = 0;
runEye = function () {
    EYES_TOTAL_TIMES = 0;
    if (!isServiceOk()) {
        loge("无障碍服务启动失败，无法作业");
        exit();
        return;
    }

    try {
        eye_readConfig();
    } catch (e) {
        loge("eyes config error {}", e);
    }
    var isPersonalMainPage = judge_isPersonalMainPage();
    if (!isPersonalMainPage) {
        loge("不在个人主页哦~");
        exit();
    }
    loge("温馨提示：适度刷眼睛");

    for (var j = 0; j < eyes_times; j++) {
        logi("=== NO.{} start ===", j + 1);
        hasClicked = 0;
        eyes_scrollForwardTimes = 0;
        hasClickedNode = [];
        while (true) {

            if (eyes_readDetailNumbers === hasClicked) {
                break;
            }

            eyes_loop(j + 1);

            if (eyes_readDetailNumbers > hasClicked) {
                // 进行翻页操作
                if (eyes_scrollForward()) {
                    eyes_scrollForwardTimes++;
                }
                logi("翻页 ヾ(•ω•`)o~~~");
                sleep(1000);
            }
        }
        if (j !== eyes_times - 1) {
            for (var c = 0; c < eyes_scrollForwardTimes; c++) {
                sleep(1000);
                eyes_scrollBackward();
                sleep(1000);
            }
        }

        logi("=== NO.{} end ===", j + 1);

        if ((j + 1) % EYES_TOTAL_SINGLE_WAIT_TIMES === 0) {
            toast("已经刷满" + EYES_TOTAL_SINGLE_WAIT_TIMES + "了，需休息" + EYES_TOTAL_SINGLE_WAIT_TIME / 1000 + "秒");
            sleep(EYES_TOTAL_SINGLE_WAIT_TIME);
        }
    }

    logi("作业结束啦 ヾ(•ω•`)o");
    logi("下次再见啦~~");
    exit();
};


runEye();
